package cn.iocoder.yudao.module.pm.service.retentionledger;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pm.controller.admin.retentionledger.vo.RetentionLedgerPageReqVO;
import cn.iocoder.yudao.module.pm.controller.admin.retentionledger.vo.RetentionLedgerSaveReqVO;
import cn.iocoder.yudao.module.pm.dal.dataobject.retentionledger.RetentionLedgerDO;
import cn.iocoder.yudao.module.pm.dal.mysql.retentionledger.RetentionLedgerMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pm.enums.ErrorCodeConstants.RETENTION_LEDGER_NOT_EXISTS;

/**
 * 留样台账管理 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class RetentionLedgerServiceImpl implements RetentionLedgerService {

    @Resource
    private RetentionLedgerMapper retentionLedgerMapper;

    @Override
    public Long createRetentionLedger(RetentionLedgerSaveReqVO createReqVO) {
        // 插入
        RetentionLedgerDO retentionLedger = BeanUtils.toBean(createReqVO, RetentionLedgerDO.class);
        retentionLedgerMapper.insert(retentionLedger);
        // 返回
        return retentionLedger.getId();
    }

    @Override
    public void updateRetentionLedger(RetentionLedgerSaveReqVO updateReqVO) {
        // 校验存在
        validateRetentionLedgerExists(updateReqVO.getId());
        // 更新
        RetentionLedgerDO updateObj = BeanUtils.toBean(updateReqVO, RetentionLedgerDO.class);
        retentionLedgerMapper.updateById(updateObj);
    }

    @Override
    public void deleteRetentionLedger(Long id) {
        // 校验存在
        validateRetentionLedgerExists(id);
        // 删除
        retentionLedgerMapper.deleteById(id);
    }

    @Override
        public void deleteRetentionLedgerListByIds(List<Long> ids) {
        // 校验存在
        validateRetentionLedgerExists(ids);
        // 删除
        retentionLedgerMapper.deleteByIds(ids);
        }

    private void validateRetentionLedgerExists(List<Long> ids) {
        List<RetentionLedgerDO> list = retentionLedgerMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RETENTION_LEDGER_NOT_EXISTS);
        }
    }

    private void validateRetentionLedgerExists(Long id) {
        if (retentionLedgerMapper.selectById(id) == null) {
            throw exception(RETENTION_LEDGER_NOT_EXISTS);
        }
    }

    @Override
    public RetentionLedgerDO getRetentionLedger(Long id) {
        return retentionLedgerMapper.selectById(id);
    }

    @Override
    public PageResult<RetentionLedgerDO> getRetentionLedgerPage(RetentionLedgerPageReqVO pageReqVO) {
        return retentionLedgerMapper.selectPage(pageReqVO);
    }

}